package com.campus.service.impl;

import com.campus.common.EasyExcelUtil;
import com.campus.dao.StudentDao;
import com.campus.dto.StudentUserExcelDto;
import com.campus.model.Student;
import com.campus.model.User;
import com.campus.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;

    // 新增学员
    @Override
    public boolean addStudentAndUser(Student student, User user) {
        return studentDao.addStudent(student,user);
    }

    @Override
    @Transactional
    public boolean batchAddStudentsWithUsersFromExcel(MultipartFile file) throws Exception {
        try {
            // 1. 解析 Excel 文件
            List<StudentUserExcelDto> excelData = EasyExcelUtil.parseExcel(file.getInputStream());

            // 2. 转换为 Student 和 User 对象
            List<Student> students = new ArrayList<>();
            List<User> users = new ArrayList<>();

            for (StudentUserExcelDto dto : excelData) {
                // 构造 Student 对象
                Student student = new Student();
                student.setStuName(dto.getStuName());

                System.out.println(dto.getStuName());

                student.setFromPlace(dto.getFromPlace());
                student.setParentPhone(dto.getParentPhone());
                student.setCurrentGrade(dto.getCurrentGrade());
                student.setIsFirst(dto.getIsFirst());
                student.setComment(dto.getComment());
                student.setStatus("0");
                students.add(student);

                // 构造 User 对象
                User user = new User();
                user.setLoginName(dto.getLoginName());
                user.setPassword(dto.getPassword());
                user.setGender(dto.getGender());
                user.setBirthday(new Date(dto.getBirthday().getTime())); // 转换为 SQL 日期
                user.setPhone(dto.getPhone());
                user.setEmail(dto.getEmail());
                user.setStatus("0"); // 默认状态为未启用
                users.add(user);
            }

            // 3. 调用 DAO 方法批量插入
            return studentDao.batchAddStudents(students, users);

        } catch (Exception e) {
            e.printStackTrace();
            throw e; // 抛出异常，触发事务回滚
        }
    }

    // 审核学员
    @Override
    public boolean updateStudentStatus(Integer studentId, String status) {
        return studentDao.updateStudentStatus(studentId, status) > 0;
    }

    // 批量审核学员
    @Override
    public boolean batchUpdateStudentStatus(List<Integer> studentIds, String status) {
        return studentDao.batchUpdateStudentStatus(studentIds, status) > 0;
    }

    @Override
    public boolean editStudent(Integer studentId, Student updatedStudent) {
        // 调用 DAO 层更新学生信息
        return studentDao.updateStudent(studentId, updatedStudent);
    }

    @Override
    public boolean deleteStudent(Integer id) {
        return studentDao.deleteStudentById(id) > 0;
    }

    @Override
    public List<Student> getAllStudents() {
        return studentDao.findAllStudents();
    }

}
